package com.kuang.unsafe;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: juc
 * @description:
 * @author: 陈恩涛
 * @create: 2021-02-02 15:22
 **/

// ConcurrentModificationException
/*
* 解决方案：
*   1、Map<String, Object> map = Collections.synchronizedMap(new HashMap<String,Object>());
*   2、ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
* */
public class MapTest {
    public static void main(String[] args) {

        // map是这样用的吗？不是，工作中不用HashMap
        // 默认等价于什么？
        // Map<String, Object> map = new HashMap<>();
        // Map<String, Object> map = Collections.synchronizedMap(new HashMap<String,Object>());
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();

        //初始化容量，加载因子

        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,5));
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
}
